var FormAdvertValidate = function () {


    return {    	
        //main function to initiate the module
        init: function () {
           

            var form = $('#form');
            var error = $('.alert-error', form);
            var success = $('.alert-success', form);                    

            form.validate({
                doNotHideMessage: true, //this option enables to show the error/success messages on tab switch.
                errorElement: 'span', //default input error message container
                errorClass: 'validate-inline', // default input error message class
                focusInvalid: false, // do not focus the last invalid input
                rules: {
                	crowdName: {
                        required: true
                    },
                    level: {
                    	required: true
                    },
                    levelUnit: {
                    	maxlength: 50,
                    	min: 1,
                    	digits:true,
                    	required: true
                    },
                    levelLimit:{
                    	maxlength: 50,
                    	min: 1,
                    	digits:true,
                    	required: true
                    },
                    returnPlan:{
                    	required: true
                    },
                    initialAmount:{
                    	number:true,
                    	min:1,
                    	required: true
                    }
                    
                    
                   /*
                    * ,
                    description: {
                    	required:true
                    },
                    riskControlDescription: {
                    	required:true
                    }
                    'payment[]': {
                        required: true,
                        minlength: 1
                    }*/
                },

              messages: { // custom messages for radio buttons and checkboxes
                    'crowdName': {
                        required: "众筹名称不能为空"
                    },'level':{
                    	required: "众筹阶段不能为空"
                    },'levelUnit':{  
                    	digits: "每份回报数量必须为整数",
                    	min:"每份回报数量必须大于0",
                    	required:"每份回报数量不能为空"
                    },'levelLimit':{
                    	digits: "分阶段可购买份数必须为整数",
                    	min: "分阶段可购买份数必须大于0"
                    },'initialAmount':{
                    	number: "投资金额必须为整数",
                    	min: "投资金额必须大于0",
                    	required:"投资金额不能为空"
                    },'returnPlan':{
                    	required:"回报项目不能为空"                    
                    }
                },

                errorPlacement: function (error, element) { // render error placement for each input type
                    if (element.attr("name") == "gender") { // for uniform radio buttons, insert the after the given container
                        error.addClass("no-left-padding").insertAfter("#form_gender_error");
                    } else if (element.attr("name") == "payment[]") { // for uniform radio buttons, insert the after the given container
                        error.addClass("no-left-padding").insertAfter("#form_payment_error");
                    } else {
                        error.insertAfter(element); // for other inputs, just perform default behavoir
                    }
                },

                invalidHandler: function (event, validator) { //display error alert on form submit   
                    success.hide();
                    error.show();
                    //App.scrollTo(error, -200);
                },

                highlight: function (element) { // hightlight error inputs
                    $(element)
                        .closest('.help-inline').removeClass('ok'); // display OK icon
                    $(element)
                        .closest('.control-group').removeClass('success').addClass('error'); // set error class to the control group
                },

                unhighlight: function (element) { // revert the change dony by hightlight
                    $(element)
                        .closest('.control-group').removeClass('error'); // set error class to the control group
                },

                success: function (label) {
                    if (label.attr("for") == "gender" || label.attr("for") == "payment[]") { // for checkboxes and radip buttons, no need to show OK icon
                        label
                            .closest('.control-group').removeClass('error').addClass('success');
                        label.remove(); // remove error label here
                    } else { // display success icon for other inputs
                        label
                            .addClass('valid ok') // mark the current input as valid and display OK icon
                        .closest('.control-group').removeClass('error').addClass('success'); // set success class to the control group
                    }
                },

                submitHandler: function (form) {
                    success.show();
                    error.hide();
                                       
                    form.submit();
                }
            });
           
        }

    };

}();